# -*- coding: utf-8 -*-

import logging

import scrapy
from scrapy.http import Request

from papa.items import ReviewItem

LOG = logging.getLogger(__name__)


class ReviewSpider(scrapy.Spider):
    name = 'review'  # unique within a project
    allowed_domains = ['movie.douban.com']
    start_urls = ['https://movie.douban.com/review/best/']

    number = 0

    def parse(self, response):
        # LOG.debug(response.text)

        reviews = response.xpath('//*[@id="content"]/div/div[1]/div[1]')
        for review in reviews.xpath('div/div[1]'):
            # LOG.debug(review.get())

            item = ReviewItem()

            movie_url = review.xpath('a[1]')[0].attrib['href']
            item['movie_id'] = movie_url.split('/')[-2]

            header = review.css('header.main-hd')[0]

            user_ele = header.xpath('a[2]')[0]
            user_url = user_ele.attrib['href']
            item['user_id'] = user_url.split('/')[-2]
            item['user_name'] = user_ele.root.text.strip()

            item['review_rate'] = None
            # 发现有影评没有评级，比如：https://www.douban.com/review/9570678/
            rate_ele = header.css('span.main-title-rating')
            if rate_ele:
                rate_class = [i for i in rate_ele[0].attrib['class'].split(' ') if i.startswith('allstar')]
                if rate_class and rate_class[0][7:].isdigit():
                    item['review_rate'] = int(rate_class[0][7:])

            item['review_time'] = header.css('span.main-meta')[0].root.text.strip()

            topic_ele = header.xpath('a[3]')
            item['topic'] = topic_ele[0].root.text.strip() if topic_ele else None

            main_ele = review.css('div.main-bd')
            review_url = main_ele.css('h2 > a').attrib['href']
            item['review_id'] = review_url.split('/')[-2]
            item['review_title'] = review.xpath('div[1]/h2[1]/a[1]')[0].root.text.strip()
            item['review_content_short'] = review.css('.short-content')[0].root.text.strip()

            count_approved_str = main_ele.css('a.up > span')[0].root.text.strip()
            item['count_approved'] = int(count_approved_str) if count_approved_str else 0
            count_opposed_str = main_ele.css('a.down > span')[0].root.text.strip()
            item['count_opposed'] = int(count_opposed_str) if count_opposed_str else 0
            item['count_comment'] = int(main_ele.css('a.reply')[0].root.text.strip(' 回应'))

            # LOG.debug('Item: %r', item)
            self.number += 1
            yield item

        # paginator = response.xpath('//*[@id="content"]/div/div[1]/div[2]')
        # LOG.debug(paginator[0].get())

        if self.number < 100:
            yield Request('https://movie.douban.com/review/best/?start=%s' % self.number, callback=self.parse)
